//
//  ListNode.swift
//  203-移除链表元素
//
//  Created by lhc on 2023/9/14.
//

import Foundation

class ListNode<T: Comparable> {
    let val: T
    var next: ListNode?
    
    init(val: T, next: ListNode? = nil) {
        self.val = val
        self.next = next
    }
    
    static func initWithList(l: [T]) -> ListNode? {
        
        guard l.count != 0 else {return nil}
        let head = ListNode(val: l.first!)
        var pre: ListNode? = head
        for i in 1..<l.count {
            pre?.next = ListNode(val: l[i])
            pre = pre?.next
        }
        return head
    }
}

extension ListNode: CustomStringConvertible {
    var description: String {
        var des = "\(self.val)"
        var cur = self
        while(cur.next != nil) {
            des += " -> \(cur.next!.val)"
            cur = cur.next!
        }
        return des
    }
}
